package com.web.manager.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.web.manager.pojo.MajorCourse;
import com.web.manager.mapper.MajorCourseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MajorCourseService  extends ServiceImpl<MajorCourseMapper, MajorCourse> {
    @Resource
    private MajorCourseMapper majorCourseMapper;

    @Override
    public List<MajorCourse> list() {
        return majorCourseMapper.list();
    }

    @Override
    public boolean save(MajorCourse entity){

        return super.save(entity);
    }


    public MajorCourse selectByUsername(String majorid){
        QueryWrapper<MajorCourse> queryWrapper = new QueryWrapper<>(); //条件查询器
        queryWrapper.eq("majorid",majorid);  // ==> where username = #{username}
        // 根据用户名查询数据库的用户信息
        return  this.getOne(queryWrapper); // ==>select * from manager_user where username = #{username}
    }



    @Transactional
    public void saveAll(List<MajorCourse> majorCourses) {

        Integer majorId = majorCourses.get(0).getMajorid();
        remove(new UpdateWrapper<MajorCourse>().eq("majorid",majorId));
        saveBatch(majorCourses);
    }

    public MajorCourse getByMajorid(Integer majorid) {
        return majorCourseMapper.getByMajorid(majorid);
    }
}
